<?php
namespace app\admin\controller;

use app\BaseController;
use think\Request;

class User extends BaseController
{
    public function login(Request $request)
    {
        try {
            $username = $request->param('username', '');
            $password = $request->param('password', '');

            if ($username == 'admin' && $password == '111111') {
                return json([
                    'code' => 20000,
                    'message' => 'OK',
                    'data' => [
                        'token' => 'admin-token'
                    ]
                ]);
            }

            return json([
                'code' => 4001,
                'message' => '用户名或密码不正确'
            ]);
        } catch (\Exception $e) {
            return json([
                'code' => 4000,
                'message' => '用户名或密码不正确'
            ]);
        }
    }

    public function info()
    {
        try {
            return json([
                'code' => 20000,
                'message' => 'OK',
                'data' => [
                    'roles' => ['admin'],
                    'introduction' => 'I am a super administrator',
                    'avatar' => 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
                    'name' => 'Super Admin'
                ]
            ]);
        } catch (\Exception $e) {
            return json([
                'code' => 4000,
                'message' => '获取用户信息失败'
            ]);
        }
    }

    public function logout()
    {
        try {
            return json([
                'code' => 20000,
                'message' => 'OK',
                'data' => 'success'
            ]);
        } catch (\Exception $e) {
            return json([
                'code' => 4000,
                'message' => '退出失败'
            ]);
        }
    }
}
